package biz

import (
	"github.com/gin-gonic/gin"
	"go-graduation/bff/internal/pkg/output"
	proto "go-graduation/pb/router"
	"google.golang.org/grpc"
	"net/http"
	"strconv"
)

func NewArticleComment(conn *grpc.ClientConn) *ArticleComment {
	return &ArticleComment{
		conn: conn,
	}
}

type ArticleComment struct {
	conn *grpc.ClientConn
}

// AddComment 添加文章评论
// 加入消息队列使用job消费
func (a *ArticleComment) AddComment(ctx *gin.Context) {
	client := proto.NewArticleCommentAddServiceClient(a.conn)
	userId, _ := strconv.Atoi(ctx.PostForm("user_id"))
	content := ctx.PostForm("content")
	req := &proto.ArticleCommentAddRequest{
		UserId:  int64(userId),
		Content: content,
	}
	res, err := client.ArticleCommentAdd(ctx, req)
	if err != nil {
		output.NewOutput().Failure(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	output.NewOutput().Success(ctx, res.Data)
	return
}
